;var RelatedSelect = function ($) {

	var NAME = 'relatedSelect';
	var DATA_KEY = 'relatedSelect';
	var JQUERY_NO_CONFLICT = $.fn[NAME];


	var Default = {
		relatedNext: 'data-related-next',
		callbacks: [],

		action: 'tab-action',
		tabListContainer: '.tab-list-container',
		tabList: ' .tab-list',
		tab: ' .tab',
		tabOpener: '',
		tabActiveClass: 'active',
		tabPanelContainer: '.tab-panel-container',
		tabPanel: 'iframe',

		attrUrl: 'href',
		attrDismiss: 'data-dismiss',
		attrTarget: 'data-target',
		tabTemplate: '<a class="tab active" href="{1}">{0}{2}</a>',
		tabClose: '<i class="fa fa-times-circle"></i>',
		panelTemplate: '<iframe src="{0}" width="100%" height="100%" frameborder="0" seamless></iframe>',
	};

	var Util = function ($) {
		function format() {
			var args = arguments;
			var str = args[0];
			return str.replace(/\{(\d+)\}/g, function (s, i) {
				return args[parseInt(i) + 1];
			});
		}

		function splitWithoutEmpty(str) {
			var result = [];
			if (typeof str != 'string') {
				return result;
			}

			var temp = str.split(' ');
			for (var i = 0, j = temp.length; i < j; i++) {
				if (temp[i] != '') {
					result.push(temp[i]);
				}
			}
			return result;
		}

		function htmlToSelector(element) {
			var elem = $(element)[0];
			var result = '';
			if (elem) {
				result += elem.tagName;
				var classArray = splitWithoutEmpty(elem.className);
				for (var i = 0, j = classArray.length; i < j; i++) {
					result += ('.' + classArray[i]);
				}
			}
			return result;
		}

		var Util = {
			format: format,
			htmlToSelector: htmlToSelector
		};

		return Util;
	}(jQuery);

	/**
	 * 支持无限级，每一级需要定义 element, action
	 *
	 * $('#level-1-select').regionSelect() 方式定义的，将通过 Default.relatedNext 来定义下一个关联元素.
	 */
	var RelatedSelect = function () {

		function RelatedSelect(element, config) {
			this._element = element;
			this._path = '';
			this._config = $.extend({}, Default, config);
			this.init();
		}

		//region 原型方法

		/**
		 * 控件初始化，主要是事件绑定
		 */
		RelatedSelect.prototype.init = function () {
			var self = this;
			var conf = this._config;
			var elem = this._element;

		};

		function getPath() {

		}

		RelatedSelect.prototype.findNext = function () {

		};

		RelatedSelect.prototype.refreshTab = function () {
			var panel = this._getTargetTabPanelContainer().find(this._config.tabPanel + ':visible');
			$(panel).attr('src', $(panel).attr('src'));
		};

		//endregion

		RelatedSelect._jQueryInterface = function (config) {
			var $elem = $(this);
			var data = $elem.data(DATA_KEY);
			if (!data) {
				data = new RelatedSelect(this, config);
				$elem.data(DATA_KEY, data);
			}

			return data;
		};

		return RelatedSelect;
	}();

	$.fn[NAME] = RelatedSelect._jQueryInterface;
	$.fn[NAME].Constructor = RelatedSelect;
	$.fn[NAME].noConflict = function () {
		$.fn[NAME] = JQUERY_NO_CONFLICT;
		return RelatedSelect._jQueryInterface;
	};
	return RelatedSelect;
}(jQuery);
